var encryptUtils = require('../../frame/utils/encrypt-utils'),
	accountModel = require('../model/account'),
	common = require('../../frame/utils/common'),
	encrypt = require('../../frame/encrypt/encrypt'),
	_ = require('underscore'),
	UserBiz = {
		create: function(accountParam, callback) {
			var param = {
				acctName: accountParam.acctName,
				acctPwd: encrypt.encrypt(accountParam.acctPwd),
				prvKey: accountParam.prvKey,
				acctTitle: accountParam.acctTitle,
				remark: accountParam.remark
			}

			accountModel.create(param, callback);
		},
		update: function(accountParam){
			var acctParam = {
				acctPwd: encrypt.encrypt(accountParam.acctPwd),
				prvKey: accountParam.prvKey,
				acctTitle: accountParam.acctTitle,
				remark: accountParam.remark
			},conditions = {
				_id: accountParam.id
			};
			return new Promise(function (resolve,reject) {
				accountModel.update(conditions,acctParam,{upsert : true},function (err,doc) {
					resolve(doc);
				});
			});
		},
		findUserByParam: function(param, callback) {
			return new Promise(function (resolve,reject) {
				accountModel.find(param).sort({accountParam: 'asc'}).exec(function (err,doc) {
					var arrAcct = _.chain(doc).map(function (acct) {
						return {
							id: acct._id.toString(),
							acctName: acct.acctName,
							acctPwd: encrypt.descrypt(acct.acctPwd),
							prvKey: acct.prvKey,
							acctTitle: acct.acctTitle,
							remark: acct.remark
						};
					}).value();
					resolve(arrAcct);
				});
			});
		},
		deleteByParam: function (param) {
			var delParam = {
				_id: param.id
			};
			return new Promise(function (resolve,reject) {
				accountModel.remove(delParam,function (err,doc) {
					resolve(doc);
				});
			});
		}
	};

module.exports = UserBiz;